# Copyright (C) 2007 Insecure.Com LLC.
#
# Authors: Guilherme Polo <ggpolo@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 
# USA

import gtk
import ConfigParser

from Base import check_instance
from Base import cairoc_to_gdkc
from Base import gdkc_to_cairoc
from Base import conf_file
from Base import color_file_std


class TL_Legend(gtk.VBox):
    """
    Set up a "Legend" for TimeLine events.
    """
    
    def __init__(self, tldaddy):
        self.tl = check_instance(tldaddy)  # TimeLine instance
        self.tl.connect('color-change-notify', self.set_colors)
        
        gtk.VBox.__init__(self)
        self.set_border_width(6)
        
        self.cbtns = { }
        
        for color, alpha, descr in self.__setup_buttons():
            cbtn = gtk.ColorButton()
            cbtn.set_color(gtk.gdk.Color(*color))
            cbtn.set_use_alpha(True)
            cbtn.set_alpha(alpha)
            cbtn.set_size_request(35, 25)
            cbtn.connect('color-set', self.color_set, descr)
            self.cbtns[descr] = cbtn
            
            hbox = gtk.HBox()
            hbox.pack_start(cbtn, False, False, 6)
            hbox.pack_start(gtk.Label(descr), False, False, 0)
            
            self.pack_start(hbox, False, False, 6)
            
        self.reset = gtk.Button("Default colors")
        self.reset.connect('clicked', self.reset_to_default)
        hbox = gtk.HBox()
        hbox.pack_start(self.reset, False, False, 6)
        self.pack_start(hbox, False, False, 6)


    def set_colors(self, event=None):
        """
        Set new colors for buttons.
        """
        for evt_d, color in self.tl.colors_evt.items():
            g_color = cairoc_to_gdkc(color[0], color[1], color[2])
            self.cbtns[evt_d].set_color(gtk.gdk.Color(*g_color))
            self.cbtns[evt_d].set_alpha(int(color[3] * 65535))


    def reset_to_default(self, event):
        """
        Set colors back to default.
        """
        cfg = ConfigParser.ConfigParser()
        cfg.read(color_file_std)
        
        p_cfg = ConfigParser.ConfigParser()
        p_cfg.read(conf_file)
        
        for sec in cfg.sections():
            if sec == "Colors":
                for opt in cfg.options(sec):
                    p_cfg.set(sec, opt, cfg.get(sec, opt))
                
                p_cfg.write(open(conf_file, 'w'))
                break
        
        self.tl.set_colors()
    
    
    def color_set(self, colorbutton, descr):
        """
        Set new color in color file.
        """
        color = colorbutton.get_color()
        alpha = colorbutton.get_alpha() / 65535.0
        cairo_c = gdkc_to_cairoc(color.red, color.green, color.blue)
        
        self.tl.queue_draw()
        
        # write probably new color to events color file
        cfg = ConfigParser.ConfigParser()
        cfg.read(conf_file)
        
        colors = { }
        if cfg.has_section("Colors"):
            cfg.set("Colors", descr, "%.3f, %.3f, %.3f, %.3f" % (cairo_c[0],
                                                        cairo_c[1], cairo_c[2],
                                                        alpha))
            cfg.write(open(conf_file, 'w'))
            
        self.tl.set_colors()


    def __setup_buttons(self):
        """
        Setup for buttons initialization.
        """
        setup = [ ]
        for evt_d, color in self.tl.colors_evt.items():
            setup.append(((cairoc_to_gdkc(color[0], color[1], color[2])),
                           int(color[3] * 65535), evt_d))
            
        return setup



if __name__ == "__main__":
    win = gtk.Window()
    win.connect('destroy', lambda w: gtk.main_quit())
    box = gtk.HBox()
    box.pack_start(TL_Legend(), False, False, 0)
    win.add(box)
    win.show_all()
    gtk.main()
